#include "CannonLayer.h"
#include "StaticData.h"
#include "Weapon.h"
using namespace cocos2d;

CannonLayer::CannonLayer()
	:_weapon(0)
	,_subItem(0)
	,_addItem(0)
{
}

CannonLayer::~CannonLayer()
{
}

bool CannonLayer::init()
{
	if(CCLayer::init())
	{
		CCSize winSize=CCDirector::sharedDirector()->getWinSize();
		_weapon=Weapon::create();
		_weapon->setPosition(winSize.width*0.5,_weapon->getCannon()->getCurCannonSprite()->getContentSize().height*0.26);
		addChild(_weapon);
		CCSprite* subItemSprite=CCSprite::createWithSpriteFrameName(StaticData::getInstance()->stringFromKey("button_sub"));
		CCSprite* addItemSprite=CCSprite::createWithSpriteFrameName(StaticData::getInstance()->stringFromKey("button_add"));
		_subItem=CCMenuItemSprite::create(subItemSprite,subItemSprite,this,menu_selector(CannonLayer::switchCannon));
		_addItem=CCMenuItemSprite::create(addItemSprite,addItemSprite,this,menu_selector(CannonLayer::switchCannon));
		CCMenu* menu=CCMenu::create(_subItem,_addItem,0);
		menu->alignItemsHorizontallyWithPadding(_weapon->getCannon()->getCurCannonSprite()->getContentSize().width+10);
		menu->setPosition(winSize.width*0.5,_subItem->getContentSize().height*0.5);
		addChild(menu);
		return true;
	}
	return false;
}

CannonLayer* CannonLayer::create()
{
	CannonLayer* aCannonLayer=new CannonLayer;
	if(aCannonLayer&&aCannonLayer->init())
	{
		aCannonLayer->autorelease();
		return aCannonLayer;
	}
	delete aCannonLayer;
	aCannonLayer=0;
	return 0;
}

void CannonLayer::switchCannon(cocos2d::CCObject* sender)
{
	CannonType type=_weapon->getCannonType();
	if(sender==_addItem){
		type=static_cast<CannonType>((int)type+1);
	}
	else if(sender==_subItem){
		type=static_cast<CannonType>((int)type-1);
	}
	_weapon->getCannon()->setType(type);
}

void CannonLayer::aimAt(CCPoint target)
{
	_weapon->aimAt(target);
}

bool CannonLayer::shootTo(CCPoint touchLocation)
{
	return _weapon->shootTo(touchLocation);
}
